//
//  xxServer_Valid.m
//  MacStockApp
//
//  Created by Andy MacPro on 12/11/4.
//
//

#import "xxServer_Valid.h"
#import "NSString+SBJSON.h"
#import "JSON.h"
#import "GBall.h"


@implementation xxServer_Valid

//檢查google代碼是否有效
+(BOOL)is_google_code_valid:(NSString *)code{
    
    NSString *urls= [NSString stringWithFormat: @"http://www.google.com/finance/info?infotype=infoquoteall&q=%@",code]  ;
    
    
    //NSLog(@"google urls=%@",urls);
    NSURL *url = [NSURL URLWithString:urls];
    
    
    NSString *userAgent = @"Bloomberg-iPad/1.0.9 CFNetwork/548.0.4 Darwin/11.0.0";
    
    NSMutableURLRequest* request = [[[NSMutableURLRequest alloc] initWithURL:url]
                                    autorelease];
    [request setValue:userAgent forHTTPHeaderField:@"User-Agent"];
    //以下可以欺騙yahoo
    //[request setValue:@"vendor=\"fff\";" forHTTPHeaderField:@"X-Client-Info"];
    
    NSURLResponse* response = nil;
    NSError* error = nil;
    NSData* data = [NSURLConnection sendSynchronousRequest:request
                                         returningResponse:&response
                                                     error:&error];
    
    NSString* aStr;
	aStr = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
    
    
    //判斷是否有內容資料
    if ([aStr isEqualToString:@""]) {
        
        [aStr release];
        return NO;
    }
    else{
        //如果有內容資料 再進一步判斷是否可以分析出一筆資料
        
        
        //去除無用的字串 '//'
        NSString *string_for_jsonvalue= [[[[[[aStr stringByReplacingOccurrencesOfString: @"//" withString:@"" ] stringByReplacingOccurrencesOfString:@"\\x27" withString:@"\'"]  stringByReplacingOccurrencesOfString:@"\\x26F" withString:@""] stringByReplacingOccurrencesOfString:@"\\x26" withString:@""] stringByReplacingOccurrencesOfString:@"\\x2" withString:@""] stringByReplacingOccurrencesOfString:@"\\x3B" withString:@""]   ;
        
        [aStr release];
        
        //將字串的每個elements 分解出來
        NSArray *jsonvalue_elements = [string_for_jsonvalue JSONValue];
        
        
        
        //分析出報價內容
        for (NSDictionary *dds in jsonvalue_elements ) {
            // NSLog(@"dds=%@",dds);
            
        }
        
        //如果可以分析出一個報價內容表示此code 有效
        if ([jsonvalue_elements count]==1) {
            return YES;
        }
        else{
            return NO;
        }
        
        
        
    }
    
    
    
    
}
//檢查yahoo代碼是否有效
+(BOOL)is_yahoo_code_valid:(NSString *)code{
    
    //加入此段是因為 yahoo的指數代碼^開頭 先將此符號轉為url看懂的%5E
    NSString *code_string=[code stringByReplacingOccurrencesOfString:@"^" withString:@"%5E"];
    
    //將代碼字串加入到ＵＲＬ
    NSString *urls= [NSString stringWithFormat: @"http://download.finance.yahoo.com/d/quotes.csv?s=%@&f=sol1hgd1t1c1p2vrj1kjdye7&e=.csv",code_string];
    
    //NSLog(@"yahoo urls=%@",urls);
    
    NSURL *url = [NSURL URLWithString:urls];
    
    
    NSMutableURLRequest* request =  [[NSMutableURLRequest alloc] initWithURL:url];
    
    
    NSURLResponse* response = nil;
    NSError* error = nil;
    NSData* data = [NSURLConnection sendSynchronousRequest:request
                                         returningResponse:&response
                                                     error:&error];
    [request release];
    
    NSString* aStr;
	aStr = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
    NSArray *csvLines = [aStr  componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
    [aStr release];
    NSString *line  ;
    NSArray *pdata ;
    //1.建構報價資料陣列
    for (int i=0; i<[csvLines count]-1; i++) {
        
        line = (NSString *)[csvLines objectAtIndex:i];
        
        if (![line isEqualToString:@""]) {
            
            line=[line stringByReplacingOccurrencesOfString:@"\"" withString:@"" ];
            
            //NSLog(@"yahoo %i line=%@",i,line);
            pdata = [line componentsSeparatedByString:@","]   ;
            
            
            if ([pdata count]>=11) {
                double d_close;
                
                d_close= [[pdata objectAtIndex:2] doubleValue];
                if (d_close==0) {
                    //表示收盤價等於0
                    
                    return NO;
                }
                else{
                    //表示收盤價不等於0
                    return YES;
                }
                
                
            }
            
            
            
            
        }
        
        
    }
    return NO;
    
    
}
//檢查bloomberg代碼是否有效
+(BOOL)is_bloomberg_code_valid:(NSString *)code{
    
    NSString *urls_bloomberg= [NSString stringWithFormat: @"http://m.bloomberg.com/iphone/apps/wds/SecurityData.asp?Securities=%@&Outfields=WBTKR,DS192,DS520,PR005,PR051,PR052,PR006,PR007,PR008,PR013,DS318,HS001,HS003,HS002,HS004,UTIME,RT012,RR900,RR902&bbipver=1.0.9,17243a4e72c7eadd0e9823cd887f97212efa43fa,iPad,iPhone%%20OS,5.0.1,zh_TW,zh-Hant",code]  ;
    //注意事項： %%
    
    //轉成 NSURL
    NSURL *url = [NSURL URLWithString:urls_bloomberg];
   
    NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:url]
    ;
    
    [request setValue:Bloomberg_User_Agent forHTTPHeaderField:@"User-Agent"];
    [request setValue:Bloomberg_Pragma forHTTPHeaderField:@"Pragma"];
    //以下可以欺騙yahoo
    //[request setValue:@"vendor=\"fff\";" forHTTPHeaderField:@"X-Client-Info"];
    //NSLog(@"main_Start bloomberg request=%@",request);
    
    NSURLResponse* response = nil;
    NSError* error = nil;
    NSData* data = [NSURLConnection sendSynchronousRequest:request
                                         returningResponse:&response
                                                     error:&error];
    [request release];
    
    NSString* aStr;
	aStr = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
     NSArray *csvLines = [aStr  componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];    
    
    [aStr release];
    
    for (NSString *line in csvLines){
        //NSLog(@"line=%@",line);
        if ([line hasPrefix:@"<px:PR005>"]) {
            
            NSString *ns=[[line stringByReplacingOccurrencesOfString:@"<px:PR005>" withString:@""] stringByReplacingOccurrencesOfString:@"</px:PR005>" withString:@""];
            
            float  close=[ns floatValue];
            
            if (close>0) {
                return YES;
            }
            else{
               return NO;
            }
            
           
           
        
            
        }
        
               
       
    }     
    
    return NO;
    
    
}

//檢查xxtradexx代碼是否有效
+(BOOL)is_xxtradexx_code_valid:(NSString *)code{

    //目前尚未建制
    return NO;

}



@end
